﻿using Newtonsoft.Json.Linq;
using System;

namespace XbmcManager.Modules
{
    public class XbmcApplication
    {
        public enum AppProperties { Volume, Muted, Name, Version };
        private String[] strAppProperties = { "volume", "muted", "name", "version" };

        private XbmcController xbmc;
        private XbmcConnection connection;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="Xbmc"></param>
        /// <param name="mConnection"></param>
        internal XbmcApplication(XbmcController Xbmc,
            XbmcConnection mConnection)
        {
            this.xbmc = Xbmc;
            this.connection = mConnection;
        }

        /// <summary>
        /// Quit XBMC
        /// </summary>
        public void Quit()
        {
            connection.SendJson("Application.Quit");
        }

        /// <summary>
        /// Toggles the mute setting.
        /// </summary>
        public void Mute()
        {
            JObject args = new JObject();
            args.Add(new JProperty("mute", "toggle"));
            connection.SendJson("Application.SetMute", args);
        }

        /// <summary>
        /// Unmute.
        /// </summary>
        public void UnMute()
        {
            JObject args = new JObject();
            args.Add(new JProperty("mute", false));
            connection.SendJson("Application.SetMute", args);
        }

        /// <summary>
        /// Adjusts XBMCs Volume
        /// </summary>
        /// <param name="level">Volume level from 0-100</param>
        public void Volume(int level)
        {
            Volume(level, false);
        }

        internal void Volume(int level, bool fromSoftMute)
        {
            if (fromSoftMute)
            {
                JObject args = new JObject();
                args.Add(new JProperty("volume", level));
                connection.SendJson("Application.SetVolume", args);
            }
            else
                xbmc.SoftMuter.VolumeChanged(level);
        }

        /// <summary>
        /// Fetches a string describing properties of XBMC
        /// </summary>
        /// <param name="name">Which property you want returned</param>
        /// <returns>A string describing the property (True/False, 0 - 100)</returns>
        public String GetProperty(AppProperties name)
        {
            string[] fields = { strAppProperties[(int)name] };

            JObject args = new JObject();
            args.Add(new JProperty("properties", fields));
            JObject response = connection.SendJson("Application.GetProperties", args);

            if (!XbmcController.isError(response)) {
                JToken items = (JToken)response["result"];
                if (name == AppProperties.Name)
                    return items["name"].ToString();
                else if (name == AppProperties.Version)
                {
                    items = items["version"];
                    return items["major"] + "." + items["minor"] + " " + items["tag"] + " rev. " + items["revision"];
                }
                else if (name == AppProperties.Muted)
                    return items["muted"].ToString();
                else if (name == AppProperties.Volume)
                    return items["volume"].ToString();
            }
            return "";
        }
    }
}
